global class scheduledVerifyCourierPickup implements Schedulable {
    global void execute(SchedulableContext sc) {
    
        List<Request__c> serviceLetterList = new List<Request__c>([Select Id, shipment_Number__c , Shipment_Status__c , Last_Courier_Update__c  from Request__c where Request_Status__c= 'Scheduled for Delivery' AND Delivery_Method__c ='Courier' ]);
        Map<string,Id> serviceLetterShipmentMap = new Map<string,Id>();
        
        Map<string,string> aramexStatusResponce = new Map<string,string>();
        Map<string,Datetime> aramexDateResponce = new Map<string,Datetime>();
        
        String param = '' ;
        String AramexResponse ='';
        if (serviceLetterList.size()>0){
              for(Aramex_Integration__c item : Aramex_Integration__c.getAll().values()){
                    param += item.AccountCountryCode__c + '|' ;
                    param += item.AccountEntity__c + '|' ;
                    param += item.AccountNumber__c + '|' ;
                    param += item.AccountPin__c + '|' ;
                    param += item.UserName__c + '|' ;
                    param += item.Password__c + '|' ;
                    param += item.Version__c ;
              }
              for(Request__c serviceLetterObject : serviceLetterList)
              {
                     param += '||'+serviceLetterObject.Shipment_Number__c;
                     serviceLetterShipmentMap.put(serviceLetterObject.Shipment_Number__c,serviceLetterObject.Id);
              }
              
            if (String.isNotBlank(param)){
                    Aramex_Smartpaperbpm.WebParam = param ; 
                    Aramex_Smartpaperbpm.CheckAramexShipment();
                    AramexResponse = Aramex_Smartpaperbpm.stdemo;
                }
          
           if (String.isNotBlank(AramexResponse)){
               for (String token : AramexResponse.split('::::' , -2)){
                  if (token.split(':::' , -2)[0].split('::' , -2)[0] == 'shipment'){                  
                      string year   = token.split(':::' , -2)[2].split('::' , -2)[1].substring(0,4);
                      string month  = token.split(':::' , -2)[2].split('::' , -2)[1].substring(5,7);
                      string day    = token.split(':::' , -2)[2].split('::' , -2)[1].substring(8,10);
                      string hour   = token.split(':::' , -2)[2].split('::' , -2)[1].substring(11,13);
                      string minute = token.split(':::' , -2)[2].split('::' , -2)[1].substring(14,16);
                      string second = token.split(':::' , -2)[2].split('::' , -2)[1].substring(17,19);
                      string stDate = year + '-' + month + '-' + day + ' ' + hour + ':' + minute +  ':' + second;
                      Datetime myDate = datetime.valueOf(stDate);
                      
                      string ShipmentStatus = token.split(':::' , -2)[1].split('::' , -2)[1];
                      string ShipmentNumber = token.split(':::' , -2)[0].split(':::' , -2)[0].substring(0,20);
                      ShipmentNumber = ShipmentNumber.substring(10,20);
                      
                      aramexStatusResponce.put(ShipmentNumber,ShipmentStatus);
                      aramexDateResponce.put(ShipmentNumber,myDate);
                  }
              }
              
              
              for (Request__c serviceObject : serviceLetterList){
              
                      if (aramexStatusResponce.containsKey(serviceObject.shipment_Number__c))
                          serviceObject.Shipment_Status__c = aramexStatusResponce.get(serviceObject.Delivery_Method__c);
                          
                      if (aramexDateResponce.containsKey(serviceObject.shipment_Number__c))
                          serviceObject.Last_Courier_Update__c = aramexDateResponce.get(serviceObject.Delivery_Method__c);     
                  
              }
              if (serviceLetterList.size()>0)
                  Update serviceLetterList; 
           }
               
         }
      }
        
    }